//
//  DBDB.m
//  豆豆
//
//  Created by 月月 on 15/4/3.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "DBDB.h"
#import <sqlite3.h>

static sqlite3 *dbPoint = nil;

@implementation DBDB

+ (sqlite3 *)openDB
{
    
    if (dbPoint) {
        return  dbPoint;
    }
    
    //把数据库文件从bundle 存储到document文件夹中
    NSFileManager *manager = [NSFileManager defaultManager];
    
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *path = [array lastObject];
    path = [NSString stringWithFormat:@"%@/%@", path, @"zhuce.db"];
    NSLog(@"%@", path);
    
    BOOL judge =  [manager fileExistsAtPath:path];
    if (judge) {
        NSLog(@"已存在");
    } else{
        NSString *string = [[NSBundle mainBundle] pathForResource:@"zhuce" ofType:@"rdb"];
        [manager copyItemAtPath:string toPath:path error:nil];
        NSLog(@"拷贝成功");
    }
    
    // 获得数据库指针
    sqlite3_open([path UTF8String], &dbPoint);
    return  dbPoint;
}

+ (void)closeDB
{
    sqlite3_close(dbPoint);
}


@end
